<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Model_Admin extends __Model {

	public $blowfish	= null;
	
	private $key64 = null, $key448 = null, $key512 = null;
	
	private $cipher		= null;
	
	private $root		= null;
	
	function __construct() {

		parent::__construct();

		$this->load->config("admin", $config);
		$this->load->module("session", $this->sess);
		
		$this->blowfish = $config['blowfish'];
		
		$this->key64	= $config['key64'];
		$this->key448	= $config['key448'];
		$this->key512	= $config['key512'];

		$this->root		= $config['root'];
		
		if ( function_exists("mcrypt_module_open") ) {
			$this->cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', 'cbc', '');
			mcrypt_generic_init($this->cipher, $this->key448, $this->key64);
		}
		unset($config);

	}

	function auth() {
		
		$member = $this->getMember();
		
		if ( isset( $member) ) return true;
		
		return false;

	}


	function getMember() {
		
		static $ret;
		
		if ( isset($ret) ) return $ret;
		
		if ( !isset($_SESSION[$this->blowfish."_auth"]) ) return null;
		
		$member = $this->memberDecode( $_SESSION[$this->blowfish."_auth"] );
		$member = @unserialize($member);
	
		if ( $member !== false ) $ret = $member;
		else $ret = null;
		
		return $ret;
	}

	function login($id, $pw) {

		$ret = 0;
		$member = array();

		
		foreach( $this->root as $v ) {
			if ( isset($v['epw']) ) {
				$epw = $this->password($pw);
				if ( $v['id'] == $id && $v['epw'] == $epw ) {
					$member['secure_mode'] = 1;
					$ret = 1;
				}
			}
			else if ( isset($v['pw']) && $v['id'] == $id && $v['pw'] == $pw ) {
				$member['secure_mode'] = 0;
				$ret = 1;
			}
		}
		
		if ( $ret ) {
			$member['id'] = $id;
			$member['log_date'] = time();
			$_SESSION[$this->blowfish."_auth"] = $this->memberEncode(serialize( $member ));
		}
		else {
			$this->logout();
		}

		return $ret;
	}
	function logout() {
		session_destroy();
		$_SESSION[$this->blowfish."_auth"] = null;
		return 1;
	}

	function memberEncode($plainData) {
		if (function_exists("mcrypt_generic")) {
			$encryptedDataOnBinary = mcrypt_generic($this->cipher, $plainData);
			return base64_encode($encryptedDataOnBinary);
		}
		else {
			return base64_encode($plainData);
		}
	}
	
	function memberDecode($encryptedData) {
		if (function_exists("mcrypt_module_open")) {
			$decryptedDataOnBinary = base64_decode($encryptedData);
			return mdecrypt_generic($this->cipher, $decryptedDataOnBinary);
		}
		else {
			return base64_decode($encryptedData);
		}
	}
	
	function password($pw) {

		$pbin = sha1($this->blowfish.$pw.$this->blowfish);
		
	 	$new = array();
	 	for($i = 0; $i < strlen($pbin); $i++) {
	 		$j = (int)($i/3);
	 		if ( !isset($new[$j]) ) $new[$j] = 0;
			$new[$j] += pow(16, ($i % 3)) * hexdec($pbin[$i]);
		}
		for ($i = 0; $i < count($new); $i++) {
			$new[$i] = $this->key512[(int)($new[$i] / 64)].$this->key512[$new[$i] % 64];
		}
	
	 	return implode("", $new);
	}
	
	function makePagination( $total, $o = array() ) {

		$defaults = array(
			"len_line" => 20,
			"len_page" => 7,
			"page_link" => "http://".SELF,
			"page"	=> 1
		);

		$settings = array_merge($defaults, $o);

		$len_line	= $settings['len_line'];
		$len_page	= $settings['len_page'];
		$page_link	= $settings['page_link'];
		$page		= $settings['page'];
		
		$max_page = max(ceil($total / $len_line), 1); // 총 몇페이지, 단 최대값은 1.
		
		$pagination = "<ul>";

		if ($max_page <= $len_page) {
			for ($i = 1; $i <= $max_page; $i++) $pagination .= "<li".(($page==$i)?" class=\"active\"":"")."><a href=\"{$page_link}?page={$i}\">{$i}</a></li>";
		}
		else {
			$pagination .= "<li".(($page==1)?" class=\"active\"":"")."><a href=\"{$page_link}?page=1\">1</a></li>";
			if ($page < $len_page - 3) {
				for ($i = 2; $i <= $len_page - 2; $i++) $pagination .= "<li".(($page==$i)?" class=\"active\"":"")."><a href=\"{$page_link}?page={$i}\">{$i}</a></li>";
				$pagination .= "<li class=\"disabled\"><span>..</span></li>";
			}
			else if ($page > $max_page - ($len_page - 3) ) {
				$pagination .= "<li class=\"disabled\"><span>..</span></li>";
				for ($i = $max_page - ($len_page - 3) ; $i < $max_page ; $i++) $pagination .= "<li".(($page==$i)?" class=\"active\"":"")."><a href=\"{$page_link}?page={$i}\">{$i}</a></li>";
			}
			else {
				$pagination .= "<li class=\"disabled\"><span>..</span></li>";
				for ($i = $page-1 ; $i < $page+2 ; $i++) $pagination .= "<li".(($page==$i)?" class=\"active\"":"")."><a href=\"{$page_link}?page={$i}\">{$i}</a></li>";
				$pagination .= "<li class=\"disabled\"><span>..</span></li>";
			}
			$pagination .= "<li".(($page==$max_page)?" class=\"active\"":"")."><a href=\"{$page_link}?page={$max_page}\">{$max_page}</a></li>";
		}

		$pagination .= "</ul>";

/////		
		return $pagination;		
	}
	
// End Class
}
